package ExerChapter16Fanxin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;


public class h1 {

    public static void main(String[] args) {

    }
    
    @Test
    public void DaoTest(){
        DAO<User> foo = new DAO<User>();

        foo.save("1001", new User(24,1001,"lisa"));

        foo.save("1002", new User(23,1002,"frank"));

        foo.save("1003", new User(20,1003,"tom"));

        System.out.println(foo.list().toString());
        
    }
}

class DAO<T>{
    Map<String,T> m = new HashMap<>();

    public void save(String id,T entry){
        m.put(id, entry);
    }
    
    public T get(String id){
        return m.get(id);
    }

    public void update(String id,T entry){
        m.put(id, entry);
    }

    public List<T> list(){
        List<T> temp = new ArrayList<>();
        for (T t : m.values()) {
            temp.add(t);
        }
        return temp;
    }

    public void delete(String id){
        m.remove(id);
    }
}


class User{
    private int age;
    private int id;
    private String name;

    public User(int age, int id, String name) {
        this.age = age;
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "[age=" + age + ", id=" + id + ", name=" + name + "]";
    }

    
    
}